//
//  YBForeignListViewController.m
//  GoodBuy
//
//  Created by Cyber on 15/5/13.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "YBForeignListViewController.h"
#import "YBForeignListCollectionViewCell.h"
#import "NetworkHandler.h"
#import "YBForeignObject.h"
#import "ForeignViewController.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "DKNightVersion.h"

@interface YBForeignListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,retain)UICollectionView *CollectionView;
@property(nonatomic,retain)NSMutableArray *ForeignArray;
@property(nonatomic,retain)NSMutableDictionary*ForeignDic;





@end

@implementation YBForeignListViewController
- (void)dealloc
{
    [_ForeignArray release];
    [_CollectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"全球买";
    self.navigationController.navigationBar.translucent = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
 
    [self getCollectionViewQ];
    [self getDataFromWeb];
    [self addHeader];
    [self addFooter];
}

-(void)getCollectionViewQ
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/3 , [UIScreen mainScreen].bounds.size.height/4);
    layout.minimumInteritemSpacing =10;
    layout.minimumLineSpacing = 90;
    layout.sectionInset = UIEdgeInsetsMake(10, 30, 120, 40);
//        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.CollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _CollectionView.backgroundColor = [UIColor whiteColor];
    _CollectionView.delegate = self;
    _CollectionView.dataSource = self;
    [self.view addSubview:_CollectionView];
    //    [_CollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [_CollectionView registerClass:[YBForeignListCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [_CollectionView release];
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _ForeignArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBForeignListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    
    YBForeignObject * YB = [[[YBForeignObject alloc] init]autorelease];
    YB = [_ForeignArray objectAtIndex:indexPath.item];
    
    
    [cell setPic:YB];
    
    
    
    return cell;
}

- (void)getDataFromWeb
{
    
    
    [NetworkHandler requestWitURL:@"http://api.smzdm.com/v1/haitao/articles?limit=20" completion:^(id result) {
        
        self.ForeignArray = [NSMutableArray array];
        NSDictionary *dic = [result objectForKey:@"data"];
        //        NSLog(@"%@",dic);
        NSArray *array = [dic objectForKey:@"rows"];
        for (NSDictionary *dic2 in array) {
            //            NSString *str = [dic2 objectForKey:@"article_pic"];
            //            NSLog(@"%@",str);
            YBForeignObject *mode = [[YBForeignObject alloc] initWithDic:dic2];
            [_ForeignArray addObject:mode];
            [mode release];
            
            
            
        }
        [self.CollectionView footerEndRefreshing];
        [self.CollectionView headerEndRefreshing];
               [_CollectionView reloadData];
        
        //[self getCollectionViewQ];
        [self.HUD removeFromSuperview];
        self.listNumber = 20;
    }];
    //小菊花
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    _HUD.labelText = @"亲 请稍等";
    _HUD.tintColor = [UIColor cyanColor];
    [_HUD showAnimated:YES whileExecutingBlock:^{
        //刷新时间最大上限
        [self showtime];
    } completionBlock:^{
        
    }];


    


 
    
}
#warning - mark - 下拉刷新
-(void)addHeader
{
      __block YBForeignListViewController *qwe = self;
     //添加下拉刷新头部控件
    [self.CollectionView addHeaderWithCallback:^{
        qwe.isUploading = NO;
        [qwe getDataFromWeb];

    }];
    
}

#warning mark - 上拉加载
-(void)addFooter
{
    __block YBForeignListViewController *qwe = self;
    //添加上拉刷新尾部控件
    [self.CollectionView addFooterWithCallback:^{
        qwe.isUploading = YES;
        //标记为上拉操作
        [qwe getDataBuy];
    }];
}

-(void)getDataBuy
{
    YBForeignObject *MOdel = [_ForeignArray lastObject];
    NSString *iderl = [NSString stringWithFormat:@"http://api.smzdm.com/v1/haitao/articles?article_date=%@&limit=20",MOdel.article_date];
    
  [NetworkHandler requestWitURL:iderl completion:^(id result) {
//      NSLog(@"%@",iderl);
      NSDictionary *dic = [result objectForKey:@"data"];
      NSArray *array = [dic objectForKey:@"rows"];
      for (NSDictionary *dic2 in array) {
          YBForeignObject *model = [[YBForeignObject alloc] initWithDic:dic2];
          [_ForeignArray addObject:model];
          [model release];
//          NSLog(@"ssss%@",_ForeignArray);
      }
      [self.CollectionView footerEndRefreshing];
      [self.CollectionView headerEndRefreshing];
      [_CollectionView reloadData];
      
  }];
}
- (void)showtime
{
    sleep(10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ForeignViewController *For = [[ForeignViewController alloc] init];
    For.listModel = [_ForeignArray objectAtIndex:indexPath.row];
    
    
    
    For.str = [_ForeignArray[indexPath.row] article_title];
    For.article_pic = [_ForeignArray[indexPath.row] article_pic];
    For.article_title = [_ForeignArray[indexPath.row]article_title];
    For.article_price = [_ForeignArray[indexPath.row]article_price];
    For.article_date = [_ForeignArray[indexPath.row]article_date];
    For.article_mall = [_ForeignArray[indexPath.row]article_mall];
    //    For.article_filter_content = [_ForeignArray[indexPath.row]article_filter_content];
    
//    For.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:For animated:YES];
//    For.hidesBottomBarWhenPushed = NO;
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(_CollectionView.contentOffset.y>1){
        TaB.hidden = YES;
        
    }else{
        TaB.hidden = NO;
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
